#! /bin/bash

umask 022

CONFIG_DIR=/data/app/jenkins/config

WS=$WORKSPACE

case `uname -s` in
    Darwin)
         NODE_OS="OSX"
         TMP_DIR=/tmp/jenkins
        TEMP_DIR=$TMP_DIR
        
        BIN_SED=/usr/local/bin/sed
        BIN_MD5=md5
        ;;
    Linux)
         NODE_OS="`uname -s`"
         TMP_DIR="/dev/shm/jenkins"
        TEMP_DIR=$TMP_DIR

        BIN_SED=sed
        BIN_MD5=md5sum
        ;;
esac

test -d $TMP_DIR || mkdir -p $TMP_DIR

BUILD_LOG=$JENKINS_HOME/jobs/$JOB_NAME/builds/$BUILD_NUMBER/log

test -f "$BUILD_LOG" && BUILD_USER=`head -1 $BUILD_LOG | awk -F '0m' '{print $2}'`

GENV_FILE_NAME=$TMP_DIR/${JOB_NAME}_${BUILD_NUMBER}

  APP_PRJ="`echo $JOB_NAME | awk -F '_' '{print $1}'`"
  APP_SUB="`echo $JOB_NAME | awk -F '_' '{print $2}'`"
  APP_ENV="`echo $JOB_NAME | awk -F '_' '{print $3}'`"
  APP_ACT="`echo $JOB_NAME | awk -F '_' '{print $4}'`"

APP_SUB_A="`echo $APP_SUB | awk -F '-' '{print $1}'`"
APP_SUB_B="`echo $APP_SUB | awk -F '-' '{print $2}'`"
APP_SUB_C="`echo $APP_SUB | awk -F '-' '{print $3}'`"
APP_SUB_D="`echo $APP_SUB | awk -F '-' '{print $4}'`"
APP_SUB_E="`echo $APP_SUB | awk -F '-' '{print $5}'`"
 
echo "$APP_ENV" | egrep -q "DEV|QA|PD"

if [ $? != 0 ] || [ -z "$APP_ENV" ]
then
    echo $Environment | egrep -q "DEV|QA|PD"
    test $? = 0 && APP_ENV=$Environment
fi

app_env="`echo $APP_ENV  | tr A-Z a-z`"
app_prj="`echo $APP_PRJ  | tr A-Z a-z `"
app_sub="`echo $APP_SUB  | tr A-Z a-z | sed 's/-/_/g'`"

PROJECT_CONFIG_DIR=$CONFIG_DIR/project/$app_prj/$app_sub

# COMPANY info
         DOMAIN=""
COMPANY_NAME_EN=""
           CNES=""

           CSST=""
### Jenkins
    JKS_RUN_USER="jenkins"
      JKS_DL_URL=
   JKS_DL_URL_19=
JKS_DOWNLOAD_URL="$JKS_DL_URL"
      JKS_DL_DIR="/data/www/downloads"
 JKS_DOCKER_HOST=

# SSH/SCP Settings
 SSH_USER=jenkins
 SSH_PORT=22
 SSH_OPTS=

SCP_SPEED=51200

RSYNC_EXCLUDE_LIST=$CONFIG_DIR/rsync-exclude.list

# WEB RUN USER
 WEB_USER=nobody
WEB_GROUP=nobody

   TOMCAT_HOME=/usr/local/tomcat
TOMCAT_WEBAPPS=$TOMCAT_HOME/webapps

# Sensitive Resources
SENSITIVE_RESOURCES="^\.svn|^\.git|\.bak$|backup$|\.tar$|\.rar$|\.gz$|\.tgz$|\.zip$|\.exe$|\.sh$|\.log$"

# Users Chack
 PERMISSION_DIR_USERS=$CONFIG_DIR/permission/users
PERMISSION_DIR_GROUPS=$CONFIG_DIR/permission/groups

ALWAYS_ALLOWED_GROUPS="ops-core qa"
        ALLOWED_USERS=""

load(){
    modules="${CONFIG_DIR}/modules/$1"

    if [ -f "$modules" ]
    then
        . "$modules"
    else
        echo "Error: File not found $modules"
        exit 1
    fi
}
